import base64
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.header import Header
from email.mime.image import MIMEImage


def construct_message(from_add, to_add, content):
    message = MIMEText(content, 'plain', 'utf-8')
    message['From'] = Header(from_add, "utf-8")
    message['To'] = Header(to_add, 'utf-8')  # 括号里的对应收件人邮箱昵称、收件人邮箱账号
    return message


# Press the green button in the gutter to run the script.
if __name__ == '__main__':
    # qq_add = "1834411262@qq.com"
    content = 'Python 邮件发送测试...'
    pku_add = "liweitj47@pku.edu.cn"
    blcu_add = "liweitj47@blcu.edu.cn"
    # smtp_host = 'smtp.qq.com'
    smtp_host = 'smtp.exmail.qq.com'
    message = construct_message(blcu_add, pku_add, content)
    password = 'Lw19931('
    # password = 'rqikkznyjtckddii'
    subject = 'Python SMTP 邮件测试'
    message['Subject'] = Header(subject, 'utf-8')
    try:
        smtp = smtplib.SMTP()
        smtp.connect(smtp_host, 25)  # 端口号这里有问题，不能按照网站说明
        # smtp.login(qq_add, password)
        smtp.login(blcu_add, password)
        # smtp.sendmail('1834411262@qq.com', ['liweitj47@blcu.edu.cn'], message.as_string())
        smtp.sendmail(blcu_add, [pku_add], message.as_string())
        print("邮件发送成功")
        smtp.quit()
    except smtplib.SMTPException:
        print("Error: 无法发送邮件")
